﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TokenManagementAppMVC.Services;

namespace TokenManagementAppMVC.Controllers
{
    [RoutePrefix("api/secure")]
    public class SecureController : ApiController
    {

        [HttpGet]
        [Route("data")]
        public IHttpActionResult GetData()
        {
            var authHeader = Request.Headers.Authorization;
            if (authHeader != null && authHeader.Scheme == "Bearer")
            {
                // 配置参数 - 实际应用中应从配置文件中读取
                string secretKey = "your-256-bit-secret"; // 至少256位(32字节)的安全密钥
                string issuer = "your-issuer";
                string audience = "your-audience";
                string userId = "user123";
                var token = authHeader.Parameter;
                var principal = JwtTokenValidator.ValidateToken(token, secretKey, issuer, audience);
                if (principal != null)
                {
                    // Token 验证成功
                    return Ok(new { Message = "This is a secure endpoint", User = principal.Identity.Name });
                }
            }

            // Token 验证失败
            return Unauthorized();
        }
    }
}
